package org.example.buysystem.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.buysystem.common.Result;
import org.example.buysystem.constants.Constants;
import org.example.buysystem.entity.Order;
import org.example.buysystem.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/order")
@CrossOrigin
@Api(value = "订单",tags = {"订单模块"})
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/batchsave")
    @ApiOperation("批量保存订单")
    public Result batchSave(@RequestBody List<Order> orders,
                            @RequestHeader("X-User-Id") Long userId) {

        String totalorderNo = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS") + RandomUtil.randomString(8);

        for (Order order : orders) {
            order.setUserId(userId);              // ✅ 从请求头中取到的 userId 设置到订单中
            order.setOrderNo(totalorderNo);       // 批次号设置
            orderService.saveOrder(order);
        }

        return Result.success(totalorderNo);
    }


    @GetMapping("/userid/{userid}")
    @ApiOperation("根据用户id查询订单")
    public Result findByUserId(@PathVariable int userid){
        return Result.success(orderService.findByUserId(userid));
    }

    @GetMapping("/delivery/{totalorderNo}")
    @ApiOperation("发货")
    public Result delivery(@PathVariable String totalorderNo){
        List<String> orderNos=orderService.findByTotalOrderNo(totalorderNo);

        for(String orderNo:orderNos){
            orderService.paid(orderNo);

        }
        return Result.success(orderNos);
    }

    @GetMapping("/received/{totalorderNo}")
    @ApiOperation("收货")
    public Result receivedOrder(@PathVariable String totalorderNo){
        if(orderService.receivedOrder(totalorderNo)){
            return Result.success();
        }else{
            return Result.error(Constants.CODE_500,"确认收货失败");
        }

    }




    @GetMapping("/paid/{totalorderNo}")
    @ApiOperation("支付订单")
    public Result payOrder(@PathVariable String totalorderNo){
        List<String> orderNos=orderService.selectByOrderNoTotal(totalorderNo);
        for (String orderNo:orderNos){
            orderService.payOrder(orderNo);
        }
        return Result.success(orderNos);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除订单")
    public Result delete(@PathVariable int id){
        orderService.removeById(id);
        return Result.success();
    }

    @GetMapping("/orderNo/{totalorderNo}")
    @ApiOperation("根据总订单号获取订单信息")
    public Result selectByOrderNo(@PathVariable String totalorderNo){
        List<String> orderNos=orderService.selectByOrderNoTotal(totalorderNo);
        Map<String,Object> map=new HashMap<>();
        for (String orderNo:orderNos){
            map=orderService.selectByOrderNo(orderNo);
        }
        return Result.success(map);


    }
}
